﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Helpers;
using Umbraco.Framework.Dynamics;
using PBDesk.com.Extension.FeedsAggregator;

namespace PBDesk.com.Extension.WebHelper
{
    public class CacheManager
    {
        #region Cache Keys
        public const string CacheKey_HomeNode = "PBDesk_HomeNode_CacheKey";
        public const string CacheKey_ArtOfTheDay = "PBDesk_ArtOfDay_cache";
        public const string CacheKey_TWC9_Item = "PBDesk_TWC9_cache";
        #endregion

        public static bool IsCustomCachingEnabled()
        {
            bool customCachingEnabled = false;
            if (bool.TryParse(ConfigurationManager.AppSettings["CustomCachingEnabled"] ?? "false", out customCachingEnabled))
                return customCachingEnabled;
            else
                return false;

        }

        #region ArticleOfTheDay
        public static BendyObject GetArticleOfTheDay()
        {
            if (IsCustomCachingEnabled())
            {
                var artOfDay = WebCache.Get(CacheManager.CacheKey_ArtOfTheDay);
                if (artOfDay != null)
                    return artOfDay;
            }
            return null;

        }
        public static void SetArticleOfTheDay(dynamic objectToCache)
        {
            if (IsCustomCachingEnabled())
            {
                WebCache.Set(CacheManager.CacheKey_ArtOfTheDay, objectToCache, MinutesTillEOD(), false);
            }
        }
        #endregion

        #region HomeNode

        public static BendyObject GetHomeNode()
        {
            if (IsCustomCachingEnabled())
            {
                var home = WebCache.Get(CacheManager.CacheKey_HomeNode);
                if (home != null)
                    return home;               
            }
            return null;

        }

        public static void SetHomeNode(dynamic objectToCache)
        {
            if (IsCustomCachingEnabled())
            {
                WebCache.Set(CacheManager.CacheKey_HomeNode, objectToCache, 60, false);
            }
        }
        #endregion

        #region HomeNode

        public static C9Item GetTWC9Item()
        {
            if (IsCustomCachingEnabled())
            {
                var c9item = WebCache.Get(CacheManager.CacheKey_TWC9_Item);
                if (c9item != null)
                    return c9item;
            }
            return null;

        }

        public static void SetTWC9Item(C9Item objectToCache)
        {
            if (IsCustomCachingEnabled())
            {
                WebCache.Set(CacheManager.CacheKey_TWC9_Item, objectToCache, MinutesTillEOD(), false);
            }
        }
        #endregion


        #region Helpers
        public static int MinutesTillEOD()
        {
            DateTime current = DateTime.Now;
            DateTime tomorrow = current.AddDays(1);
            DateTime eod = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day);
            TimeSpan timeSpan = eod - current;
            return (int)timeSpan.TotalMinutes;
        }
        #endregion
    }
}
